<?php
//    +----------------------------------------------------------------------
//    | Copyright (c) 2015-2035 http://www.hdphp.cn All rights reserved.
//    | Licensed ( http://www.hdphp.cn/licenses/ )
//    | Author: Jack <sophia2152@qq.com>
//    | 官方网址: http://www.hdphp.cn
//    | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//    | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//    +----------------------------------------------------------------------
namespace app\services\system;

use app\services\BaseServices;
use app\dao\system\SystemMenusDao;
use app\services\system\admin\SystemAdminRoleServices;
use hdphp\utils\FormatArray;

/**
 * Class：SystemMenusServices
 * 创建人：Jack
 * 创建时间：2023/3/21 7:48 PM
 * @package app\services\system
 * @mixin SystemMenusDao
 */
class SystemMenusServices extends BaseServices
{
    /**
     * SystemMenusServices constructor.
     * @param SystemMenusDao $dao
     */
    public function __construct(SystemMenusDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 注释：获取菜单没有被修改器修改的数据
     * 创建人：Jack
     * 创建时间：2023/3/23 16:56
     * @param $menusList
     * @param int $type
     * @return array
     */
    public function getMenusData($menusList, int $type = 1)
    {
        $data = [];
        foreach ($menusList as $item) {
//            $item['expand'] = true;
            $item['selected'] = false;
            $item['title'] = $item['menu_name'];
            $item['menu_path'] = preg_replace('/^\/' . ($this->type[$type] ?? 'admin') . '/', '', $item['menu_path']);
            $data[] = $item->getData();
        }
        return $data;
    }

    /**
     * 注释：getMenusList
     * 创建人：Jack
     * 创建时间：2023/3/23 16:53
     * @param $rouleId
     * @param int $level
     * @param int $type
     * @return array
     */
    public function getMenusList($rouleId, int $level, int $type = 1)
    {
        /** @var SystemAdminRoleServices $systemAdminRoleServices */
        $systemAdminRoleServices = app()->make(SystemAdminRoleServices::class);
        $rules = $systemAdminRoleServices->getRoleArray(['status' => 1, 'id' => $rouleId], 'rules');
        $rulesStr = FormatArray::unique($rules);
        $menusList = $this->dao->getMenusRoule(['type' => $type, 'route' => $level ? $rulesStr : '']);
        $unique = $this->dao->getMenusUnique(['type' => $type, 'unique' => $level ? $rulesStr : '']);

        return [FormatArray::getMenuIviewList($this->getMenusData($menusList, $type)), $unique];
    }


}
